package com.zh.mp.demo.modules.demo.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.mp.demo.exception.NotFoundException;
import com.zh.mp.demo.modules.demo.entity.User;
import com.zh.mp.demo.modules.demo.mapper.UserMapper;
import com.zh.mp.demo.modules.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.introspector.Property;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-09-27
 */
@DS("slave")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    @Cacheable(cacheNames = "user", key = "'USER:'+#id", unless = "#result == null")
    public User getById(Long id) {
        User user = super.getById(id);
        return user;
    }

    @Override
    @DS("master")
    public int insert(User user){
        return userMapper.insert(user);
    }

    @Override
    @DS("master")
    @CacheEvict(cacheNames = "user", key = "'USER:'+#id")
    public boolean removeById(Long id){
        return super.removeById(id);
    }

    @Override
    @DS("master")
    @CachePut(cacheNames = "user", key = "'USER:'+#user.id", unless = "#result == null")
    public User updateUser(User user){
        userMapper.updateById(user);
        return super.getById(user.getId());
    }

    public User queryOne(AbstractWrapper wrapper){
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public Page<User> pageList(int pageSize, int limit, AbstractWrapper wrapper){
        //  参数一：当前页
        //  参数二：页面大小
        //  使用了分页插件之后，所有的分页操作也变得简单的！
        Page<User> page = new Page<>(pageSize, limit);
        userMapper.selectPage(page,wrapper);

        return page;
    }

    @Override
    public Page<User> pageList(int pageSize, int limit){
        //  参数一：当前页
        //  参数二：页面大小
        //  使用了分页插件之后，所有的分页操作也变得简单的！
        Page<User> page = new Page<>(pageSize, limit);
        userMapper.selectPage(page,null);

        return page;
    }
}
